using System;
using System.IO;
using System.Globalization;
using System.Text;
using System.Web;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.Framework;
using log4net;
using ud_Article.Business;
using mojoPortal.Web;

namespace ud_Article.UI
{

    public partial class AttachmentDownload : NonCmsBasePage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(AttachmentDownload));

        #region OnInit
        override protected void OnInit(EventArgs e)
        {
            Load += Page_Load;
            
            base.OnInit(e);
        }
        #endregion

        private int pageID = -1;
        private int moduleID = -1;
        private int fileID = -1;
        readonly DBArticleAttachment repository = new DBArticleAttachment();
        private ud_ArticleAttachment attachment;
        


        protected void Page_Load(object sender, EventArgs e)
        {
            SecurityHelper.DisableDownloadCache();
            //Server.ScriptTimeout

            if (
                (LoadAndCheckParams())
                && (UserHasPermission())
                )
            {
                DownloadFile();
            }
            else
            {
                if (!Request.IsAuthenticated)
                {
                    SiteUtils.RedirectToLoginPage(this);
                    return;
                }
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }

        }

        private void DownloadFile()
        {
            if ((CurrentPage == null) || (attachment == null)) return;
            string downloadPath = Page.Server.MapPath(WebUtils.GetApplicationRoot() 
                                                      + "/Data/ArticleAttachments/") 
                                  + attachment.ServerFileName;

            if (File.Exists(downloadPath))
            {
                FileInfo fileInfo = new FileInfo(downloadPath);
                Page.Response.AppendHeader("Content-Length", fileInfo.Length.ToString(CultureInfo.InvariantCulture));
            }
            else
            {
                log.Error("Shared File Not Found. User tried to download file " + downloadPath);
                return;
            }

// ReSharper disable PossibleNullReferenceException
            string fileType = Path.GetExtension(attachment.FileName).Replace(".", string.Empty);
// ReSharper restore PossibleNullReferenceException

            string mimeType = SiteUtils.GetMimeType(fileType);
            Page.Response.ContentType = mimeType;

            if (SiteUtils.IsNonAttacmentFileType(fileType))
            {
                //this will display the pdf right in the browser
                Page.Response.AddHeader("Content-Disposition", "filename=\"" + HttpUtility.UrlEncode(attachment.FileName, Encoding.UTF8) + "\"");
            }
            else
            {
                // other files just use file save dialog
                Page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(attachment.FileName, Encoding.UTF8) + "\"");
            }

            //Page.Response.AddHeader("Content-Length", documentFile.DocumentImage.LongLength.ToString());

            try
            {
                Page.Response.Buffer = false;
                Page.Response.BufferOutput = false;
                if (Page.Response.IsClientConnected)
                {
                    Page.Response.TransmitFile(downloadPath);
                    repository.IncreaseDownloadCount(fileID);
                }
                   
            }
            catch (HttpException) { }
            Page.Response.End();
        }

        private bool UserHasPermission()
        {
            bool result = false;

            if (
                (CurrentPage != null)
                &&(WebUser.IsInRoles(CurrentPage.AuthorizedRoles))
                )
            {
                bool moduleIsOnPage = false;
                
                foreach (Module m in CurrentPage.Modules)
                {
                    if (m.ModuleId == moduleID)
                    {
                        moduleIsOnPage = true;
                        // user has page viewpermission but not module view permission so his module is no visible on the page for this user.
                        if (!WebUser.IsInRoles(m.ViewRoles)) { return false; }

                    }

                }

                if (moduleIsOnPage)
                {
                    attachment = repository.GetOne(fileID);

                    if (attachment.ModuleID == moduleID) result = true;
                }

            }

            return result;

        }

        private bool LoadAndCheckParams()
        {
            bool result = true;

            pageID = WebUtils.ParseInt32FromQueryString("pageid", -1);
            moduleID = WebUtils.ParseInt32FromQueryString("mid", -1);
            fileID = WebUtils.ParseInt32FromQueryString("fileid", -1);
            if (pageID == -1) result = false;
            if (moduleID == -1) result = false;
            if (fileID == -1) result = false;

            return result;

        }

        
    }
}
